package edu.hawaii.teams.halealohacli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * Tests the CurrentPower class.
 * 
 * @author Branden Ogata
 * 
 */
public class TestCurrentPower {

  /**
   * Tests the getData method in the CurrentPower class.
   * 
   * @throws Exception Handles any errors that do occur, such as timeouts.
   * 
   */
  @Test
  public void testGetData() throws Exception {
    // Constant double used to convert from watt to kilowatt.
    final double KILO = 1000.0;
    final String testSource = "Ilima-A";

    // Creates data to compare the test data to
    WattDepotClient client = new WattDepotClient("http://server.wattdepot.org:8190/wattdepot/");
    CurrentPower sample = new CurrentPower();
    double controlData = client.getLatestPowerConsumed(testSource) / KILO;

    // Ensure that the current power according to the CurrentPower class
    // is roughly equivalent to the current power according to a call in this test class
    assertTrue(
        "Testing CurrentPower for correct value",
        Math.abs(controlData - Double.valueOf(sample.getData(client, testSource)) / KILO)
        > (controlData * 0.05));

    // Check the case with multiple inputs
    assertFalse("Testing CurrentPower with multiple inputs",
        sample.getData(client, testSource, "Lehua-A").contains("Error:"));

    // Check what happens with invalid client
    assertEquals("Testing CurrentPower with invalid client",
        sample.getData(new WattDepotClient("client/"), "Ilima-A"),
        "Error: An error occurred while retrieving data.\n\n");

    // Check what happens with invalid source
    assertFalse("Testing CurrentPower with invalid source", sample.getData(client, "Alpha-A")
        .contains("Error: The source Alpha could not be found.\n\n"));

    // Check that the data returned is positive
    assertTrue("Testing CurrentPower for positive value",
        Double.valueOf(sample.getData(client, testSource)) > 0);
  }
}